package cn.tuling.org.service.impl;

import cn.tuling.basic.util.PageList;
import cn.tuling.org.domain.Novel;
import cn.tuling.org.mapper.NovelMapper;
import cn.tuling.org.query.NovelQuery;
import cn.tuling.org.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class NovelServiceImpl implements NovelService {

    @Autowired
    private NovelMapper novelMapper;

    @Override
    @Transactional
    public void addNovel(Novel novel) {
        novelMapper.addNovel(novel);
    }

    @Override
    @Transactional
    public void updateNovel(Novel novel) {
        novelMapper.updateNovel(novel);

    }

    @Override
    @Transactional
    public void deleteNovel(Long id) {
        novelMapper.deleteNovel(id);

    }

    @Override
    public List<Novel> findAll() {
        return novelMapper.findAll();
    }

    @Override
    public Novel findById(Long id) {
        return novelMapper.findById(id);
    }

    @Override
    public PageList<Novel> queryByPage(NovelQuery novelQuery) {
        Integer totals = novelMapper.queryByCount(novelQuery);
        if (totals == null || totals == 0){
            return new PageList<>(0,new ArrayList<>());
        }
        List<Novel> novels = novelMapper.queryByPage(novelQuery);
        return new PageList<>(totals,novels);
    }
}
